<!--
 * @FilePath: /web-vue/src/views/House/addForm.vue
 * @Description: 【房源】新增表单
-->
<template>
  <div>
    <!-- 弹出层-表单 -->
    <el-dialog
      class="add-dialog"
      title="新增【房源】"
      :visible.sync="visible"
      :confirmLoading="confirmLoading"
      top="7vh"
      :close-on-click-modal="false"
      :close-on-press-escape="true"
      :fullscreen="true"
    >
      <el-form
        :model="addForm"
        :rules="rules"
        ref="addForm"
        status-icon
        label-width="150px"
        style="width: 100%"
        class="layer-form-style"
      >
        <el-form-item label="房间标题：" prop="houseTitle" size="mini">
          <el-input
            v-model="addForm.houseTitle"
            placeholder="请输入房间标题"
            clearable
            style="width: 99%"
          ></el-input>
        </el-form-item>
        <el-form-item label="地区：" prop="regionId" size="mini">
          <el-cascader
            v-model="addForm.regionId"
            :options="$store.state.DICT.regionCode"
            :props="{
              expandTrigger: 'hover',
              checkStrictly: true,
              emitPath: false,
              value: 'id',
              label: 'name',
            }"
            placeholder="请选择地区"
            filterable
            clearable
            style="width: 99%"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="具体位置：" prop="detailLocation" size="mini">
          <el-input
            v-model="addForm.detailLocation"
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 9 }"
            placeholder="请输入具体位置"
            clearable
            style="width: 99%"
          ></el-input>
        </el-form-item>
        <el-form-item label="房源类型：" prop="houseType" size="mini">
          <el-select
            v-model="addForm.houseType"
            filterable
            clearable
            placeholder="请选择房源类型"
            style="width: 99%"
          >
            <el-option
              v-for="item in $store.state.DICT.houseTypeSelect"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配套服务设施：" prop="provide" size="mini">
          <el-input
            v-model="addForm.provide"
            placeholder="请输入配套服务设施"
            clearable
            style="width: 99%"
          ></el-input>
        </el-form-item>
        <el-form-item label="房间详情：" prop="detail" size="mini">
          <ckeditor v-model="addForm.detail" style="width: 99%"></ckeditor>
        </el-form-item>
        <el-form-item label="单价/天：" prop="cost" size="mini">
          <el-input-number
            v-model="addForm.cost"
            :step="50"
            :min="0"
            placeholder="请输入单价/天"
            clearable
          ></el-input-number>
        </el-form-item>
        <el-form-item label="折扣：" prop="discount" size="mini">
          <el-input-number
            v-model="addForm.discount"
            :step="0.1"
            :precision="2"
            :max="1"
            :min="0"
            placeholder="请输入折扣"
            clearable
          ></el-input-number>
        </el-form-item>
        <el-form-item label="房源数量：" prop="inventory" size="mini">
          <el-input-number
            v-model="addForm.inventory"
            :step="1"
            :min="0"
            placeholder="请输入房源数量"
            clearable
          ></el-input-number>
        </el-form-item>
        <el-form-item label="入住要求：" prop="command" size="mini">
          <el-input
            type="textarea"
            v-model="addForm.command"
            :autosize="{ minRows: 3, maxRows: 9 }"
            placeholder="请输入入住要求"
            clearable
            style="width: 99%"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          icon="el-icon-finished"
          @click="handleSubmit()"
          >保存
        </el-button>
        <el-button icon="el-icon-refresh-left" @click="handleCancel()"
          >取消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  DateTimeRange_Picker_Option,
  DateRange_Picker_Option,
  Date_Picker_Option,
} from "@/utils/datePicker";

export default {
  components: {},

  data() {
    return {
      visible: false,
      confirmLoading: false,
      addForm: {
        houseTitle: null,
        regionId: null,
        detailLocation: null,
        houseType: null,
        provide: null,
        detail: null,
        cost: null,
        discount: null,
        command: null,
        inventory: null,
      },
      // 表单校验规则
      rules: {
        houseTitle: [
          {
            required: true,
            message: "房间标题不能为空！",
            trigger: "blur",
          },
        ],
        regionId: [
          {
            required: true,
            message: "地区不能为空！",
            trigger: "blur",
          },
        ],
        detailLocation: [
          {
            required: true,
            message: "具体位置不能为空！",
            trigger: "blur",
          },
        ],
        houseType: [
          {
            required: true,
            message: "房源类型不能为空！",
            trigger: "blur",
          },
        ],
        cost: [
          {
            required: true,
            message: "单价/天不能为空！",
            trigger: "blur",
          },
        ],
        inventory: [
          {
            required: true,
            message: "房源数量不能为空！",
            trigger: "blur",
          },
        ],
      },
      // 日期选择快捷方式
      DateTimeRange_Picker_Option,
      DateRange_Picker_Option,
      Date_Picker_Option,
    };
  },

  methods: {
    // 打开【房源】表单新增窗口
    openAddForm(isShow) {
      this.visible = isShow;
    },

    // 关闭【房源】表单新增窗口
    handleCancel() {
      this.visible = false;
      this.$refs["addForm"].resetFields();

      // 清空上传文件列表
    },

    // 提交
    handleSubmit() {
      this.$refs["addForm"].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            target: document.querySelector(".add-dialog"),
            lock: true,
            text: "正在保存，请耐心等待！",
          });

          // 请求参数
          let param = Object.assign({}, this.addForm);

          // 发起请求
          this.$api
            .addHouse(param)
            .then((res) => {
              loading.close();
              let { status, message, data } = res.data; // 响应数据
              if (status == 200) {
                this.$emit("afterSubmit");
                this.handleCancel();
                // 提示信息
                this.$message({
                  type: "success",
                  message: `操作成功！已新增【房源】!${message}`,
                  duration: 1680,
                });
              } else if (status == 403) {
                this.$message({
                  message: `权限不足！${message}`,
                  type: "error",
                  center: true,
                  duration: 1680,
                });
                // this.$router.push("/login");
              } else {
                this.$alert(`操作失败！${message}`, "【房源】新增", {
                  type: "error",
                  confirmButtonText: "确定",
                  callback: (action) => {},
                });
              }
            })
            .catch((err) => {
              loading.close();
              this.visible = false;
              console.log(`【房源】新增接口发生错误：${err}`);
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
// 表单布局样式
@import "@/assets/css/form-layout.scss";
</style>
